﻿namespace Probel.NDoctor.Statistics.Console.Commands
{

    using Probel.NDoctor.Statistics.Console.Menus;
    using Probel.NDoctor.Statistics.Console.Properties;

    internal class MySqlGetStatCommand : Command
    {
        #region Methods

        protected override void SafeExecute()
        {
            var s = Settings.Default;
            Output.Write("Connecting to the server...");
            var scalar = Queries
                .ForMySql(s.Server, s.Database, s.User, s.Password)
                .Scalars();
            Output.WriteLine("OK");

            Output.Write("Retrieving data...");
            var active = scalar.GetActiveApplicationCount();
            var applications = scalar.GetApplicationCount();
            var avgExec = scalar.GetAvgExecutionTime();
            var durations = scalar.GetDurationCount();
            var firstInstall = scalar.GetFirstInstallation();
            var lastInstall = scalar.GetLastInstallation();
            var lastUpdate = scalar.GetLastUpdate();
            var statistics = scalar.GetStatisticsCount();
            Output.WriteLine("OK");

            Output.WriteLine("Overview:");
            Output.EmptyLine();
            Output.WriteLine("Active application count: {0}", active);
            Output.WriteLine("       Application count: {0}", applications);
            Output.WriteLine("        Statistics count: {0}", statistics);
            Output.WriteLine("          Duration count: {0}", durations);
            Output.WriteLine("  average execution time: {0:0.00} msec", avgExec);
            Output.WriteLine("           First install: {0}", firstInstall.ToShortDateString());
            Output.WriteLine("            Last install: {0}", lastInstall.ToShortDateString());
            Output.WriteLine("             Last update: {0}", lastUpdate.ToShortDateString());
        }

        #endregion Methods
    }
}